﻿package com.ws.components.player{
	import flash.display.*;
	import flash.events.*;
	import flash.events.Event;
	import flash.utils.Dictionary;
	
	public class Animation extends Bitmap{
		private var _fps:int = 8;
		private var totalFrames:int;
		private var enterFrameCountor:Number;
		private var currentFrame:int;
		private var playDoneCallBack:Function;
		private var content:Array;
		private var _spanFrame :Number ;
		
		public function Animation(pic:Bitmap,col:int,row:int){
			content = ImgeCovert.divide(pic,col,row);
			bitmapData = content[0];			
		}

		public function set fps(value:int):void{	
			_fps = value;
		}
		
		public function start():void{
			enterFrameCountor = 0;
			currentFrame = 0;
			_spanFrame = (this.stage.frameRate / _fps);
			addEventListener(Event.ENTER_FRAME,enterFrame);
		}
		public function enterFrame(e:Event):void{
			enterFrameCountor ++;
			if(enterFrameCountor % _spanFrame ==0)
			{
				currentFrame++;
				var len:int = content.length;
				if(currentFrame >= len){
					//animation done
					removeEventListener(Event.ENTER_FRAME,enterFrame);
					if(playDoneCallBack != null)
						playDoneCallBack();
				}else{					
					bitmapData = content[currentFrame];
				}
			}
		}
	}
}